String handling



Learning Objectives : Student should be able to -


String is the data type to store text. Every string contains a number of characters and each character can be labelled by its position number. The first character in a string can be in position zero or position one, depending on the language.

String handling methods are as follows :

⇒  LENGTH - returns the integer value representing the length of string. The identifier should be of data type string.
For example, LENGTH("Computer Science") will return "16".

⇒  SUBSTRING(<identifier>, <start>, <length>) - returns a string of length length starting at position start. The identifier should be of data type string, length and start should be positive, and data type integer.
For example, SUBSTRING("Computer Science", "10", "7") will return "Science".

⇒  UCASE - returns the string/character with all characters in upper case. The identifier should be of data type string or char.
For example, UCASE(Computer Science) will return "COMPUTER SCIENCE".

⇒  LCASE - returns the string/character with all characters in upper case. The identifier should be of data type string or char.
For example, LCASE(Computer Science) will return "computer science".

Q1.  Write pseudocode statements to perform the following string handling operations on the variable MyString.

a)  Write pseudocode statements to  declare the variables  MyString.

DECLARE MyString : STRING

b)  Write pseudocode statements to  store the string Writing computer program is an efficient skill” in the variable MyString.

MyString = "Writing computer program is an efficient skill"

c)  Write pseudocode statements to  find the length  of the characters stored in the variable MyString.

LENGTH(MyString)

d)  Write pseudocode statements to  convert the data  stored in the variable MyString in  upper case. 

UCASE(MyString)

e)  Write pseudocode statements to  convert the data  stored in the variable MyString in  lower case. 

LCASE(MyString)

f)  Write pseudocode statements to  find the first character  of data stored in the variable MyString.

SUBSTRING(MyString, 1, 1)

g)  Write pseudocode statements to  extract the word  "computer" from the data stored in the variable MyString.

SUBSTRING(MyString, 9, 8)

Q2.  A variable X, Y and Z are used to store data in a program.

 a)  Write pseudocode statements to declare the variables X, Y and Z.

DECLARE  X : STRING

DECLARE  Y : INTEGER

DECLARE  Z : INTEGER

 b)  The function Length(X) finds the length of a string X.

The function Substring(X, Y, Z) finds a substring of X starting at position Y and Z characters long. The first character in X is position 1.

Write pseudocode statements to :

X ‹— “Programming is fun”

Y ‹— 16

Z ‹— 3

LengthOfString ‹— LENGTH(X)

OUTPUT “The length of the string is ”, LengthOfString

FindWord ‹— SUBSTRING(X, Y, Z)

OUTPUT “Extracted word is : ”, FindWord

Q3.  Write pseudocode algorithm to input a word -

DECLARE  MyString : STRING
OUTPUT  "Enter the word : "
INPUT  MyString
IF  LENGTH(MyString) = 8  AND  MyString = UCASE(MyString) THEN
OUTPUT  “The word meets the rules”
ELSE
OUTPUT  “Invalid word”
ENDIF

Q4.  Write pseudocode algorithm to input a password, find and output how many characters are upper-case and lower-case alphabets.

Assume that the password does not contain any numbers or symbols.

NoUp ‹— 0
NoLow ‹— 0
OUTPUT  "Enter the password : "
INPUT  Password
FOR Counter = 1 TO LENGTH(Password)
SingleChar ‹— SUBSTRING(Password, Counter, 1)
IF  SingleChar = UCASE(SingleChar)  THEN  NoUp ‹— NoUp + 1
IF  SingleChar = LCASE(SingleChar)  THEN  NoLow ‹— NoLow + 1
NEXT  Counter
OUTPUT  "Number of upper-case letter is ",  NoUp
OUTPUT  "Number of lower-case letter is ",  NoLow

Q5.  Write pseudocode algorithm to input a password, find and output how many characters are numeric digits between 0 and 9.

Digits ‹— "0123456789"
NoDigit ‹— 0
OUTPUT  "Enter the password : "
INPUT  Password
// Loop for each character of the password
FOR Count = 1 TO LENGTH(Password)
SingleChar ‹— SUBSTRING(Password, Counter, 1)
// Loop for each digit stored as string in Digits
FOR D = 1 TO LENGTH(Digits)
SingleDigit ‹— SUBSTRING(Digits, D, 1)
IF SingleChar = SingleDigit THEN NoDigit = NoDigit + 1
NEXT  D
NEXT  Counter
OUTPUT  "Number of numeric digits in the password is ",  NoDigit



* * * * * * * * *
* * * * * *
* * *
*

>>